package com.wandian.login.mvvm.userlogin;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.JsonObject;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.BaseBean;
import com.mylib.libcore.bean.UserInfoBean;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.DevicesIDUtil;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.crc.AESUtils;
import com.wandian.login.bean.LoginDataBean;
import com.wandian.login.data.BaseAppApi;

import java.util.HashMap;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.NetConfig.BASE_SUCCESS_CODE;

/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description LoginActivityViewModel
 **/
public class LoginActivityViewModel extends BaseViewModel {

    private BaseAppApi baseAppApi;

    public LoginActivityViewModel(@NonNull Application application) {
        super(application);
        baseAppApi = RetrofitUtil.getAppRetrofit().create(BaseAppApi.class);
    }


    //设备登录
    public void loginByDevice(String deviceNum){
        showDialog();
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("deviceNum", deviceNum);
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        String key = AESUtils.getInstance().encrypt(hashMap);
        //  String key2 = RC4Util.encry(JsonUtil.toJson(hashMap));
        Log.d("crc_aeskey",key);
        Log.d("crc_aeskey_dec",AESUtils.getInstance().decrypt(key));
        //   Log.d("crc_rc4key_dec",RC4Util.decry(key2));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.fastRegister(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        dismissDialog();
                        Log.d("fastRegister",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            String json = AESUtils.getInstance().decrypt(bean.getData());
                            Log.d("fastRegister",   json);
                            if(!UIUtils.isEmpty(bean.getData())){
                                 LoginDataBean loginBean = (LoginDataBean)JsonUtil.parseJsonToBean(AESUtils.getInstance().decrypt(bean.getData()),LoginDataBean.class);
                                 if(!UIUtils.isEmpty(loginBean)){
//                                     AppContext.getInstance().getAppPreferencesHelper().setKeyValue(MINE_ZL,loginBean.getSurplusNum()+"") ;
//                                     AppContext.getInstance().getAppPreferencesHelper().setKeyValue(CHALLENGE_ZL_NUM,loginBean.getNum()+"");
                                     AppContext.getInstance().getAppPreferencesHelper().setToken(loginBean.getWdt());;
                                     AppContext.getInstance().getAppPreferencesHelper().setKeyValue("isLogin","1");
                                     AppContext.getInstance().getAppPreferencesHelper().setKeyValue("user_id",loginBean.getId()+"");
                                     getUserInfo();
                                }else {
                                     ToastUtil.show("账号异常");
                                 }
                            }else {
                                ToastUtil.show("账号异常");
                            }
                        }else {
                            ToastUtil.show("账号异常");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                        Log.d("fastRegister",   JsonUtil.toJson(e));
                        if(!UIUtils.isEmpty(e.getMessage())){
                            ToastUtil.show(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //微信登录
    public void loginByWX(String deviceNum,String code){
        showDialog();
        if(UIUtils.isEmpty(deviceNum))deviceNum = "wd_did_"+System.currentTimeMillis();
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("deviceNum", deviceNum);
        hashMap.put("code", code);
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        String key = AESUtils.getInstance().encrypt(hashMap);
        //  String key2 = RC4Util.encry(JsonUtil.toJson(hashMap));
        Log.d("crc_aeskey",key);
        Log.d("crc_aeskey_dec",AESUtils.getInstance().decrypt(key));
        //   Log.d("crc_rc4key_dec",RC4Util.decry(key2));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.wxLogin(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        dismissDialog();
                        Log.d("wxLogin",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            String json = AESUtils.getInstance().decrypt(bean.getData());
                            Log.d("wxLogin",   json);
                            if(!UIUtils.isEmpty(bean.getData())){
                                LoginDataBean loginBean = (LoginDataBean)JsonUtil.parseJsonToBean(AESUtils.getInstance().decrypt(bean.getData()),LoginDataBean.class);
                                if(!UIUtils.isEmpty(loginBean)){
//                                     AppContext.getInstance().getAppPreferencesHelper().setKeyValue(MINE_ZL,loginBean.getSurplusNum()+"") ;
//                                     AppContext.getInstance().getAppPreferencesHelper().setKeyValue(CHALLENGE_ZL_NUM,loginBean.getNum()+"");
                                    AppContext.getInstance().getAppPreferencesHelper().setToken(loginBean.getWdt());;
                                    AppContext.getInstance().getAppPreferencesHelper().setKeyValue("isLogin","1");
                                    AppContext.getInstance().getAppPreferencesHelper().setKeyValue("user_id",loginBean.getId()+"");
                                    getUserInfo();
                                    ARouter.getInstance().build(RouterConfig.ROUTER_MAIN).navigation();
                                    finish();
                                }else {
                                    ToastUtil.show("账号异常");
                                }
                            }else {
                                ToastUtil.show("账号异常");
                            }
                        }else {
                            ToastUtil.show("账号异常");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                        Log.d("fastRegister",   JsonUtil.toJson(e));
                        if(!UIUtils.isEmpty(e.getMessage())){
                            ToastUtil.show(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    public void getUserInfo(){
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.getUserInfo(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        Log.d("getUserInfo",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            try {
                                if(!UIUtils.isEmpty(bean.getData())){
                                    Log.d("crc_rc4key_getVersion", AESUtils.getInstance().decrypt(bean.getData()));
                                    UserInfoBean userInfoBean = JsonUtil.parseJsonToBean(AESUtils.getInstance().decrypt(bean.getData()),UserInfoBean.class);
                                    if(!UIUtils.isEmpty(userInfoBean)){
                                        AppContext.getInstance().getAppPreferencesHelper().setUserInfoBean(userInfoBean);
                                        goToMainActivity();
                                    }
                                }else  goToMainActivity();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }else goToMainActivity();
                    }

                    @Override
                    public void onError(Throwable e) {
                        goToMainActivity();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void goToMainActivity(){
        ARouter.getInstance().build(RouterConfig.ROUTER_MAIN).navigation();
        finish();
    }

    public void clinkForgetPassword(View view){
        finishhideSoftBoard(view);
        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_FORGETPASSWORD).navigation();
    }
    public void clinkRegist(View view){
        finishhideSoftBoard(view);
        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_REGIST).navigation();
    }
}
